package view.screen;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import view.base.CanvasScreen;
import base.ScreenActionListener;
import controller.Controller;
import data.ConversationRecorder;
import data.Message;

public final class MessengerScreen extends CanvasScreen{

	private final int HORIZONTALDRAWPTR = 7;
	private final int HORIZONTALNAMEDRAWPTR = 3;

	private final int width;
	
	private final int height;

	private final Image img;

	private final String contact;

	private final ConversationRecorder conversation;

	private int verticalDrawPtr;

	private final Font font;

	public MessengerScreen(ScreenActionListener controller, String contact, Image img, ConversationRecorder cr ){
		super(controller);

		width = this.getWidth();

		height = this.getHeight();
		
		this.img = img; 

		this.contact = contact;

		this.conversation = cr;

		this.font = Font.getDefaultFont();

		verticalDrawPtr = 0;

		this.addCommand(new Command("Send", Command.OK, 1));

		this.setCommandListener(this);
	}
	
	protected void paint(Graphics g) {
		//primeiro
		g.setColor(255,255,255);
		g.fillRect(0,0, width, height);
		char[] sentence;
		int begin = 0;
		int end = 1;
		String user;
		Message[] messages = conversation.getConversation();
		g.drawImage(img, 0, verticalDrawPtr,0);
		verticalDrawPtr += img.getHeight()/3;
		g.setColor(0, 0, 255);
		g.drawString(contact, img.getWidth() + HORIZONTALNAMEDRAWPTR, verticalDrawPtr, 0);
		verticalDrawPtr = img.getHeight()+img.getHeight()/5;
		for(int i = 0 ; i<messages.length; ++i){
		if (messages[i] == null) {
			verticalDrawPtr = 0;
			return;
		}
		if(messages[i].getUser() != null){
			g.setColor(255, 0, 0);
			user = messages[i].getUser();
		}
		else{
			g.setColor(0,0,255);
			user = contact;
		}
		if(verticalDrawPtr <= img.getHeight() || verticalDrawPtr + font.getHeight()*2 > height){
			verticalDrawPtr = 0;
			break;
		}
		g.drawString(user+ " says:", HORIZONTALNAMEDRAWPTR, verticalDrawPtr, 0);
		verticalDrawPtr += font.getHeight();
		sentence = messages[i].getMessage().toCharArray();
		while(end != 0){
			if(verticalDrawPtr <= img.getHeight()){
				break;
			}
			end = formatSentence(sentence, begin);
			if(end == 0)
				break;
			g.drawChars(sentence, begin, end-begin, HORIZONTALDRAWPTR, verticalDrawPtr, 0);
			verticalDrawPtr += font.getHeight();
			begin = end;
		}
		begin = 0;
		end = 1;
		}
	verticalDrawPtr = 0;
	}

	private int formatSentence(char[] sentence, int ptr){
		if(ptr == sentence.length)
			return 0;
		int lastSpace = 0;
		int i;
		for(i = ptr; i<sentence.length; ++i){
			if(sentence[i] == ' ')
				if(font.charsWidth(sentence, ptr, i-ptr) < width-HORIZONTALDRAWPTR)
					lastSpace = i;
				else
					return lastSpace;
		}
		if(i == sentence.length)
			return i;
		return 0;
	}

	public void commandAction(Command c, Displayable d) {
		switch (c.getCommandType()) {
		case Command.OK:
			controller.viewAction(Controller.SEND_COMMAND, null);
			break;
		default:
			super.commandAction(c, d);
			break;
		}
	}

}
